using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;

using LongNet.Util;

namespace LongNet.Web
{
    /// <summary>
    /// A helper class to manage to http sessions for convenience.
    /// This class cannot be inherited.
    /// </summary>
    public static class HttpSessions
    {
        #region Fields

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the timeout of session in minutes.
        /// </summary>
        public static int Timeout
        {
            get
            {
                var context = HttpContext.Current;
                if (context != null)
                    return context.Session.Timeout;

                return 0;
            }
            set
            {
                var context = HttpContext.Current;
                if (context != null)
                    context.Session.Timeout = value > 0 ? value : 20;
            }
        }

        /// <summary>
        /// Gets the unique token current session state.
        /// </summary>
        public static string SessionId
        {
            get
            {
                var context = HttpContext.Current;
                if (context != null)
                    return context.Session.SessionID;

                return string.Empty;
            }
        }

        #endregion

        #region Static Constructor

        static HttpSessions()
        {

        }

        #endregion

        #region Static Methods

        #region Set

        /// <summary>
        /// Sets a session value by name.
        /// </summary>
        /// <param name="name">The name of the session.</param>
        /// <param name="value">The value of the session.</param>
        public static void Set(string name, string value)
        {
            name.ThrowsIfNullOrEmpty("name", "The name is null or empty.");

            var context = HttpContext.Current;
            if (context != null)
            {
                context.Session[name] = value;
            }
        }

        #endregion

        #region Get

        /// <summary>
        /// Gets a session value by name.
        /// </summary>
        /// <param name="name">The name of cookie.</param>
        public static object Get(string name)
        {
            name.ThrowsIfNullOrEmpty("name", "The name is null or empty.");

            var context = HttpContext.Current;
            if (context != null)
            {
                return context.Session[name];
            }

            return null;
        }

        #endregion

        #region Remove/Abandon/Clear

        /// <summary>
        /// Removes a session by name.
        /// </summary>
        /// <param name="name">The name of cookie.</param>
        public static void Remove(string name)
        {
            name.ThrowsIfNullOrEmpty("name", "The name is null or empty.");

            var context = HttpContext.Current;
            if (context != null)
            {
                context.Session.Remove(name);
            }
        }

        /// <summary>
        /// Abandons the current session states.
        /// </summary>
        public static void Abandon()
        {
            var context = HttpContext.Current;
            if (context != null)
                context.Session.Abandon();
        }

        /// <summary>
        /// Clears all key/value of the current session states.
        /// </summary>
        public static void Clear()
        {
            var context = HttpContext.Current;
            if (context != null)
                context.Session.Clear();
        }

        #endregion

        #endregion
    }
}
